package mware_access;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class MiddlewareService { // - Zentrale API der Middleware -

    private static NameService nc;
    private static String host;
    private static int nsPort, objektPort;
//    private List<KontoSkeleton> kntSkeletons = new ArrayList<KontoSkeleton>();
//    private List<ManagerSkeleton> mngSkeletons = new ArrayList<ManagerSkeleton>();
    private static Logger logger;

    /**
     * Initializes the middleware classes and returns a middlware objekt
     *
     * @param serviceHost on which the nameservice can be reaced
     * @param listenPort on which the nameservice can be reaced
     * @return a middlewareService object
     */
    public static MiddlewareService init(String serviceHost, int listenPort) {
        host = serviceHost;
        nsPort = listenPort; // Kommunikation zum NS
        objektPort = nsPort + 1; //um jedem Objekt ein Port zur Kommunikation zu geben

        // logger holen
        logger = getLogger();

        MiddlewareService mws = new MiddlewareService();
        nc = new NameServiceProxy(mws);
        logger.log(Level.INFO, "Middleware initialisiert.");
        return mws;
    }

    /** Initialisieren der Middleware vor ihrer Benutzung.
     * Das zurueckgelieferte Objekt soll fuer den Aufrufer
     * zentraler Dienstanbieter der Middleware sein.
     * Parameter: Host und Port, bei dem die zentralen Dienste
     * (Namensdienst) kontaktiert werden sollen.
     */
    public NameService getNameService() {
        return nc;
    }

    public void increasePort() {
        objektPort++;
    }

    public int getNSPort() {
        return nsPort;
    }

    public String getHost() {
        return host;
    }

    public int getPort() {
        return objektPort;
    }

    /**
     * @param message
     * @return
     */
    public String send(String message) {
        Socket socket;
        BufferedReader dataIn;
        DataOutputStream dataOut;
        try {
            socket = new Socket(host, nsPort);
            dataIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            dataOut = new DataOutputStream(socket.getOutputStream());
            dataOut.writeBytes(message);
            return dataIn.readLine();
        } catch (UnknownHostException e) {
        } catch (IOException e) {
        } catch (NumberFormatException e) {
        }
        return null;
    }

    /**
     * Returns a single logger for the middleware package
     * 
     * @return a logger for the middleware package
     */
    public static Logger getLogger() {
        FileHandler fhandler = null;
        if (logger == null) {
            try {
                fhandler = new FileHandler("MWare_Access.log");
                fhandler.setFormatter(new SimpleFormatter());
                logger = Logger.getLogger("MWare_Access");
                logger.addHandler(fhandler);
                logger.setLevel(Level.INFO);
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return logger;
    }
}
